#include <iostream>
#include <math.h>


using namespace std;
#define E  1*10e-6;


int main()
{
  float simpleequation(float a ,float b);
  float quadraticequation(float a ,float b,float c);
  float complexequation(float a,float b,float c,float d);
  int n;
  cout<<"input the number:n(1-3)"<<endl;
  cin>>n;
  cout<<"the order of equation="<<n<<endl;
  if (n==1)
  {
     float coeff1,coeff2;
     cout<<"input two numbers:"<<endl;
     cin>>coeff1>>coeff2;
     simpleequation(coeff1,coeff2);
  }
  else if (n==2)
  {
       float coeff3,coeff4,coeff5;
       cout<<"input three numbers:"<<endl;
       cin>>coeff3>>coeff4>>coeff5;
       quadraticequation(coeff3,coeff4,coeff5);

   }
   else if (n==3)
   {
       float coeff6,coeff7,coeff8,coeff9;
       cout<<"input four paras:"<<endl;
       cin>>coeff6>>coeff7>>coeff8>>coeff9;
       complexequation(coeff6,coeff7,coeff8,coeff9);
   }
return 0;
}

float simpleequation(float a,float b)
{   float x;
    if (a!=0)
        {x=-b/a ;
        cout<<"the equation result="<<x<<endl;}
     else if ((a==0)&&(b==0))
        cout<<"the equation has  infinitude results"<<endl;
     else if ((a==0)&&(b!=0))
        cout<<"the equation has no result"<<endl;
     return 0;
}

float quadraticequation(float a,float b,float c)
{
    float x1,x2,tm,pm,ym,rm;
    tm=b*b-4*a*c;
    if (a==0)
         simpleequation(b,c);
    else if ((tm>=0)&&(a!=0))
        {pm=sqrt(tm);
        x1=(-b+pm)/(2*a);
        x2=(-b-pm)/(2*a);
        cout<<"the equation has two real result:x1="<<x1<<",x2="<<x2<<endl;}
    else if ((tm<0)&&(a!=0))
        {
        ym=(-b)/(2*a);
        rm=sqrt(-tm)/(2*a);
        cout<<"the equation has two umresl result:x1="<<ym<<"+j*"<<rm<<",x2="<<ym<<"+j*"<<-rm<<endl;}
   return 0;
}

float complexequation(float a,float b,float c,float d)//newton method
{
        float x0,g,f,x;
        cout<<"input one init number:x0"<<endl;
        cin>>x0;
        do
          {  x=x0;
             f=a*x0*x0*x0+b*x0*x0+c*x0+d;
             g=3*a*x0*x0+2*b*x0+c;
             x0=x-f/g;
          } while ((fabs(x-x0))>E);
      cout<<"the result of equation is the det="<<x0<<endl;
      return 0;
}

